package com.cuvretool.view.jpanel;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;

import com.cuvretool.View;
import com.cuvretool.utils.Vector2;
import com.cuvretool.view.BaseJPanel;
import com.cuvretool.view.coordinate.GridDrawInfo;
import com.cuvretool.view.coordinate.RulerDrawInfo;
import com.cuvretool.view.coordinate.workspace.TransformWorkSpace;
import com.cuvretool.view.inputlistener.ViewInputHandleListener;
import com.cuvretool.view.mousekeyboard.workspace.KeyboardEventWorkSpace;
import com.cuvretool.view.mousekeyboard.workspace.MouseEventWorkSpace;
/**
 * 绘制区域
 * since 2015年1月14日 下午10:37:04
 * @author boy
 */
public class JPanelWorkSpace extends BaseJPanel {

	// ===========================================================
	// Constants
	// ===========================================================
	private static final long serialVersionUID = 1L;
	// ===========================================================
	// Fields
	// ===========================================================
	/**
	 * 窗口外抛处理方式至View的接口
	 */
	private ViewInputHandleListener mViewInputHandleListener;
	/**坐标转换方式定义*/
	private TransformWorkSpace mTransformWorkSpace = null;
	/**网格绘制需求参数结构定义*/
	private GridDrawInfo mGridDrawInfo;
	/**标尺绘制需求参数结构定义*/
	private RulerDrawInfo mRulerDrawInfo;
	// ===========================================================
	// Constructors
	// ===========================================================
	
	// ===========================================================
	// Getter & Setter
	// ===========================================================

	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================
	
	@Override
	public void init() {
		super.init();
		mViewInputHandleListener = View.getViewInputHandleListener();
		
		mGridDrawInfo = new GridDrawInfo();
		mGridDrawInfo.mBeginX = 30;
		mGridDrawInfo.mBeginY = 20;
		mGridDrawInfo.mTotalX = this.getWidth()-mGridDrawInfo.mBeginX;
		mGridDrawInfo.mTotalY = this.getHeight()-mGridDrawInfo.mBeginY;
		mGridDrawInfo.setGridNumberX(10);
		mGridDrawInfo.setGridNumberY(15);
		mGridDrawInfo.run();
		
		mRulerDrawInfo = new RulerDrawInfo();
		mRulerDrawInfo.setmGridDrawInfo(mGridDrawInfo);
		mRulerDrawInfo.init();
		mRulerDrawInfo.run();
		mViewInputHandleListener.mRulerDrawInfo = mRulerDrawInfo;
		
		mTransformWorkSpace = new TransformWorkSpace();
		mTransformWorkSpace.setDisplaySize(this.getWidth(), this.getHeight());
		mTransformWorkSpace.setGridDrawInfo(mGridDrawInfo);
		mViewInputHandleListener.mTransformWorkSpace = mTransformWorkSpace;
		
		MouseEventWorkSpace mouseEvent = new MouseEventWorkSpace();
		mouseEvent.setBaseJPanel(this);
		mouseEvent.setRulerDrawInfo(mRulerDrawInfo);
		this.addMouseListener(mouseEvent);
		this.addMouseMotionListener(mouseEvent);
		this.addMouseWheelListener(mouseEvent);
		
		KeyboardEventWorkSpace keyboardEvent = new KeyboardEventWorkSpace();
		keyboardEvent.setBaseJPanel(this);
		this.addKeyListener(keyboardEvent);
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		super.run();
	}

	@Override
	public void over() {
		// TODO Auto-generated method stub
		super.over();
	}

	@Override
	public void resize() {
		mTransformWorkSpace.setDisplaySize(this.getWidth(), this.getHeight());
		
		mGridDrawInfo.mTotalX = this.getWidth()-mGridDrawInfo.mBeginX;
		mGridDrawInfo.mTotalY = this.getHeight()-mGridDrawInfo.mBeginY;
		mGridDrawInfo.setType(GridDrawInfo.OnGridSpace);
	}

	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		if(mTransformWorkSpace == null) return;
		mTransformWorkSpace.TransformDrawToDisplay();
		DrawGrid(g);
		DrawRuler(g);
		Draw(g);
	}
	
	// ===========================================================
	// Methods
	// ===========================================================
	/**
	 * 绘制workspace区的网格
	 * @param g
	 */
	private void DrawGrid(Graphics g){
		g.setColor(Color.BLACK);
		ArrayList<Vector2> temp = mTransformWorkSpace.mPostionsDisplayX;
		Vector2 vec1,vec2;
		for (int i = 0; i < temp.size(); i++) {
			vec1 = temp.get(i);
			vec2 = temp.get(++i);
			g.drawLine((int)vec1.x, (int)vec1.y, (int)vec2.x, (int)vec2.y);
		}
		
		temp = mTransformWorkSpace.mPostionsDisplayY;
		for (int i = 0; i < temp.size(); i++) {
			vec1 = temp.get(i);
			vec2 = temp.get(++i);
			g.drawLine((int)vec1.x, (int)vec1.y, (int)vec2.x, (int)vec2.y);
		}
	}
	/**
	 * 绘制左边和下面标尺值
	 * @param g
	 */
	private void DrawRuler(Graphics g){
		mRulerDrawInfo.run();
		
		g.setColor(Color.YELLOW);
		ArrayList<Vector2> temp = mTransformWorkSpace.mPostionsDisplayX;
		ArrayList<String> str = mRulerDrawInfo.mRulerX;
		Vector2 vec;
		for (int i = 0; i < temp.size(); i++) {
			vec = temp.get(i++);
			g.drawString(str.get(i/2),(int)vec.x, (int)vec.y+15);
		}
		
		temp = mTransformWorkSpace.mPostionsDisplayY;
		str = mRulerDrawInfo.mRulerY;
		for (int i = 0; i < temp.size(); i++) {
			vec = temp.get(i++);
			g.drawString(str.get(i/2),(int)vec.x-30, (int)vec.y);
		}
	}
	/**
	 * 外抛绘制方式
	 * @param g
	 */
	private void Draw(Graphics g){
		mViewInputHandleListener.WorkSpaceDraw(g);
	}
	// ===========================================================
	// Inner and Anonymous Classes
	// ===========================================================
}
